package com.fulu.game.bi.service.impl.play;

import cn.hutool.core.date.DateUtil;
import com.fulu.game.bi.common.util.csv.CsvExportUtil;
import com.fulu.game.bi.common.util.csv.Mapper;
import com.fulu.game.bi.common.util.csv.Querier;
import com.fulu.game.bi.common.util.math.MathUtils;
import com.fulu.game.bi.entity.bo.channel.GameOverviewListBO;
import com.fulu.game.bi.entity.enums.QueryTypeStatusEnum;
import com.fulu.game.bi.entity.vo.play.GameOverviewListVO;
import com.fulu.game.bi.entity.vo.play.GameOverviewRealtimeVO;
import com.fulu.game.bi.hbase.HBaseGameOverviewDAO;
import com.fulu.game.bi.redis.service.play.GameOverviewService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GameOverviewServiceImpl extends BasePlayService implements GameOverviewService {

    @Autowired
    private HBaseGameOverviewDAO hBaseGameOverviewDAO;

    @Override
    public List<GameOverviewRealtimeVO> realtime() {
        GameOverviewRealtimeVO today = hBaseGameOverviewDAO.findByParameter(DateUtil.today());
        today = today == null ? new GameOverviewRealtimeVO() : today;
        GameOverviewRealtimeVO yesterday = hBaseGameOverviewDAO.findByParameter(DateUtil.formatDate(DateUtil.offsetDay(new Date(), -1)));
        yesterday = yesterday == null ? new GameOverviewRealtimeVO() : yesterday;
        GameOverviewRealtimeVO theDayBeforeYesterday = hBaseGameOverviewDAO.findByParameter(DateUtil.formatDate(DateUtil.offsetDay(new Date(), -2)));
        theDayBeforeYesterday = theDayBeforeYesterday == null ? new GameOverviewRealtimeVO() : theDayBeforeYesterday;
        GameOverviewRealtimeVO lastWeekYesterdaySameDay = hBaseGameOverviewDAO.findByParameter(DateUtil.formatDate(DateUtil.offsetDay(new Date(), -8)));
        lastWeekYesterdaySameDay = lastWeekYesterdaySameDay == null ? new GameOverviewRealtimeVO() : lastWeekYesterdaySameDay;
        List<GameOverviewRealtimeVO> list = new ArrayList<>();
        yesterday.setNewAddPalyeeNumDay(MathUtils.chainRatio(yesterday.getNewAddPalyeeNum(), theDayBeforeYesterday.getNewAddPalyeeNum()));
        yesterday.setNewAddPalyeeNumWeek(MathUtils.chainRatio(yesterday.getNewAddPalyeeNum(), lastWeekYesterdaySameDay.getNewAddPalyeeNum()));
        yesterday.setLivelyPlayeeNumDay(MathUtils.chainRatio(yesterday.getLivelyPlayeeNum(), theDayBeforeYesterday.getLivelyPlayeeNum()));
        yesterday.setLivelyPlayeeNumWeek(MathUtils.chainRatio(yesterday.getLivelyPlayeeNum(), lastWeekYesterdaySameDay.getLivelyPlayeeNum()));
        yesterday.setOrderPlayeeNumDay(MathUtils.chainRatio(yesterday.getOrderPlayeeNum(), theDayBeforeYesterday.getOrderPlayeeNum()));
        yesterday.setOrderPlayeeNumWeek(MathUtils.chainRatio(yesterday.getOrderPlayeeNum(), lastWeekYesterdaySameDay.getOrderPlayeeNum()));
        yesterday.setRefundPlayeeNumDay(MathUtils.chainRatio(yesterday.getRefundPlayeeNum(), theDayBeforeYesterday.getRefundPlayeeNum()));
        yesterday.setRefundPlayeeNumWeek(MathUtils.chainRatio(yesterday.getRefundPlayeeNum(), lastWeekYesterdaySameDay.getRefundPlayeeNum()));
        yesterday.setOrderUserNumDay(MathUtils.chainRatio(yesterday.getOrderUserNum(), theDayBeforeYesterday.getOrderUserNum()));
        yesterday.setOrderUserNumWeek(MathUtils.chainRatio(yesterday.getOrderUserNum(), lastWeekYesterdaySameDay.getOrderUserNum()));
        yesterday.setOrderNumDay(MathUtils.chainRatio(yesterday.getOrderNum(), theDayBeforeYesterday.getOrderNum()));
        yesterday.setOrderNumWeek(MathUtils.chainRatio(yesterday.getOrderNum(), lastWeekYesterdaySameDay.getOrderNum()));
        yesterday.setRefundUserNumDay(MathUtils.chainRatio(yesterday.getRefundUserNum(), theDayBeforeYesterday.getRefundUserNum()));
        yesterday.setRefundUserNumWeek(MathUtils.chainRatio(yesterday.getRefundUserNum(), lastWeekYesterdaySameDay.getRefundUserNum()));
        yesterday.setRefundNumDay(MathUtils.chainRatio(yesterday.getRefundNum(), theDayBeforeYesterday.getRefundNum()));
        yesterday.setRefundNumWeek(MathUtils.chainRatio(yesterday.getRefundNum(), lastWeekYesterdaySameDay.getRefundNum()));
        yesterday.setRewardUserNumDay(MathUtils.chainRatio(yesterday.getRewardUserNum(), theDayBeforeYesterday.getRewardUserNum()));
        yesterday.setRewardUserNumWeek(MathUtils.chainRatio(yesterday.getRewardUserNum(), lastWeekYesterdaySameDay.getRewardUserNum()));
        yesterday.setRewardNumDay(MathUtils.chainRatio(yesterday.getRewardNum(), theDayBeforeYesterday.getRewardNum()));
        yesterday.setRewardNumWeek(MathUtils.chainRatio(yesterday.getRewardNum(), lastWeekYesterdaySameDay.getRewardNum()));
        yesterday.setRewardMoneyAllDay(MathUtils.chainRatio(yesterday.getRewardMoneyAll(), theDayBeforeYesterday.getRewardMoneyAll()));
        yesterday.setRewardMoneyAllWeek(MathUtils.chainRatio(yesterday.getRewardMoneyAll(), lastWeekYesterdaySameDay.getRewardMoneyAll()));
        yesterday.setOrderMoneyAllDay(MathUtils.chainRatio(yesterday.getOrderMoneyAll(), theDayBeforeYesterday.getOrderMoneyAll()));
        yesterday.setOrderMoneyAllWeek(MathUtils.chainRatio(yesterday.getOrderMoneyAll(), lastWeekYesterdaySameDay.getOrderMoneyAll()));
        list.add(today);
        list.add(yesterday);
        return list;
    }


    @Override
    public List<GameOverviewListVO> list(GameOverviewListBO listBO) {
        listBO.setTableName(gameOverviewListStat.get(listBO.getQueryType()));
        List<GameOverviewListVO> list = hBaseGameOverviewDAO.list(listBO);
        return list.stream().map(map -> {
            map.setQueryType(listBO.getQueryType());
            return map;
        }).collect(Collectors.toList());
    }

    @Override
    public void export(GameOverviewListBO bo) {
        String fileName = "语音互动-总览";
        LinkedHashMap<String, Mapper<GameOverviewListVO>> map = new LinkedHashMap<>();
        QueryTypeStatusEnum anEnum = QueryTypeStatusEnum.getEnum(bo.getQueryType());
        switch (anEnum) {
            case DAY:
                map.put("日期", v -> v.getStartDate());
                break;
            default:
                map.put("日期", v -> v.getStartDate() + "-" + v.getEndDate());
                break;
        }
        map.put("新增陪玩师", v -> v.getNewAddPalyeeNum() + "");
        map.put("活跃陪玩师", v -> v.getLivelyPlayeeNum() + "");
        map.put("接单陪玩师", v -> v.getOrderPlayeeNum() + "");
        map.put("退款陪玩师", v -> v.getRefundPlayeeNum() + "");
        map.put("下单人数", v -> v.getOrderUserNum() + "");
        map.put("下单次数", v -> v.getOrderNum() + "");
        map.put("退款人数", v -> v.getRefundUserNum() + "");
        map.put("退款次数", v -> v.getRefundNum() + "");
        map.put("下单金额", v -> v.getOrderMoneyAll().toString());
        map.put("下单时长", v -> v.getOrderDurationTime().toString());
        map.put("优惠券使用人数", v -> v.getCouponUserNum() + "");
        map.put("优惠券使用金额", v -> v.getCouponMoneyAll().toString());
        map.put("打赏人数", v -> v.getRewardUserNum() + "");
        map.put("打赏次数", v -> v.getRewardNum() + "");
        map.put("打赏金额", v -> v.getRewardMoneyAll().toString());
        Querier querier = (num) -> {
            PageInfo pageInfo = new PageInfo(list(bo));
            pageInfo.setHasNextPage(false);
            return pageInfo;
        };
        CsvExportUtil.export(100, fileName, map, querier);
    }


}
